package com.baymax.test.scene.demo

import android.graphics.Color
import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.newFlexContainer
import com.baymax.test.scene.newSceneFlexContainer
import com.baymax.test.scene.newScrollContainer
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.properties.Delegates

/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.demo
 * @class:       DrawOrderScene
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2022/11/29 16:02
 * @Copyright (C) 2022 YSTEN
 * @author:       xuyujian
 */
class DrawOrderScene : BaseScene() {
    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View =
        p1.newScrollContainer {
            val gridLayoutManager = GridLayoutManager(p1.context, 4)

            var gridCount by Delegates.observable(4) { p, o, n ->
                gridLayoutManager.spanCount = n
            }
            this.newFlexContainer {
                addButton("change grid count") {
                    gridCount = gridCount.plus(1).mod(8).coerceAtLeast(4)
                }

            }
            val c = p1.context
            val rv = RecyclerView(c)
            rv.addItemDecoration(object : ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    itemPosition: Int,
                    parent: RecyclerView
                ) {
                    return outRect.set(6, 6, 6, 6)
                }
            })
            addView(rv)
            class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
                val txt = itemView as TextView

                init {
                    txt.apply {
                        minHeight = 160
                        textSize = 20f
                        gravity =Gravity.CENTER
                        setTextColor(Color.RED)
                        setBackgroundColor(Color.YELLOW)
                        isFocusable = true
                        setOnFocusChangeListener { v, hasFocus ->
                            if (hasFocus) {
                                rv.postInvalidate()
                                v.setBackgroundColor(Color.BLUE)
                                v.animate().scaleX(1.3f).scaleY(1.3f).setDuration(300).start()
                            } else {
                                setBackgroundColor(Color.YELLOW)
                                v.animate().scaleX(1f).scaleY(1f).setDuration(300).start()
                            }
                        }
                    }
                }
            }
            rv.setChildDrawingOrderCallback { childCount, i ->
                return@setChildDrawingOrderCallback rv.focusedChild?.let {
                    val fi = rv.indexOfChild(it)
                    if (fi == i) childCount - 1 else if (i < fi) i else fi + childCount - 1 -i
                    // 焦点前的item绘制顺序不变
//                    return@let if (i < fi) {
//                        i
//                    } else {
//                        //  上焦的item最后绘制,
//                        // 焦点前的item绘制顺序不变,
//                        // 焦点后的item绘制顺序-1
//                        if (i < childCount - 1) fi + childCount - 1 - i else fi
//                    }
                }?.also { index ->
                    Log.d(TAG, "setChildDrawingOrderCallback: $i, $index ")
                    rv.getChildAt(i)?.apply {
                        (this as TextView).text = "D:$index"
                    }
                } ?: i
            }
            rv.layoutManager = gridLayoutManager
            rv.adapter = object : RecyclerView.Adapter<VH>() {
                override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH =
                    VH(TextView(c))

                override fun onBindViewHolder(holder: VH, position: Int) {
                    //holder.txt.text = "P:$position"
                }

                override fun onBindViewHolder(
                    holder: VH,
                    position: Int,
                    payloads: MutableList<Any>
                ) {
                    super.onBindViewHolder(holder, position, payloads)
                    println("payloads $payloads")
                    payloads.forEach {
                        when{
                            it is String && it.startsWith("request") -> holder.itemView.requestFocus()
                        }
                    }
                }

                override fun getItemCount(): Int = 12
            }.also {
                it.notifyDataSetChanged()
                rv.post { it.notifyItemChanged(3 ,"request2") }
            }



        }
}